<template>
    <div>
        <div class="header">
            <div class="logo" @click="go()" style="cursor: pointer;"><img src="../../assets/logo.png" style="width: 95%;" /></div>
            <div class="headers-right-right">
                <div class="block">
                    <el-avatar shape="square" size="large" :src="squareUrl"></el-avatar>
                </div>
                <el-dropdown size="medium" @command="handleCommand">
                    <span class="el-dropdown-link">
                        {{ username }}<i class="el-icon-arrow-down el-icon--right"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="github">修改密码</el-dropdown-item>
                        <el-dropdown-item divided command="quit">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </div>
        <el-dialog title="修改密码" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="旧密码" prop="password">
                    <el-input v-model="form.password" placeholder="请输入旧密码"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="newPassword">
                    <el-input v-model="form.newPassword" placeholder="请输入新密码"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitFun()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
    
</template>
<script>
export default {
    data() {
        return {
            squareUrl: "https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png",
            dialogVisible: false,
            form: {
                password: '',
                newPassword: ''
            },
            rules: {
                password: [
                    { required: true, message: '请输入旧密码', trigger: 'blur' },
                    { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
                ],
                newPassword: [
                    { required: true, message: '请输入新密码', trigger: 'blur' },
                    { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
                ],
            },
            username: '',//登录用户名称
        }
    },
    created() {
        this.username = this.$store.state.user.name;
    },
    methods: {
        handleClose(done) {
            done();
        },
        go(){
            this.$router.push({path:"/"})
        },
        submitFun() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    this.isdisabled = true;
                    this.Sevice({
                        url: 'sys/user/password',
                        method: 'post',
                        data: {
                            'password': this.form.password,
                            'newPassword': this.form.newPassword,
                        },
                    }).then(res => {
                        this.isdisabled = false;
                        if (res && res.code == 200) {
                            this.dialogVisible = false;
                            this.$message({
                                message: res.msg,
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.$emit('getRolelist')
                                }
                            })
                        }
                    }).catch(err => {
                        console.log(err);
                        this.isdisabled = false;
                    })
                }
            })
        },
        // 用户名 下拉菜单
        handleCommand(command) {

            switch (command) {
                case 'github':
                    this.dialogVisible = true;
                    this.$nextTick(() => {
                        this.$refs.form.resetFields();
                        this.isdisabled = false;
                    })
                    // window.open('https://github.com/cgq001/admin-menu/tree/2.0')
                    break;
                case 'quit':
                    let src = this.$store.dispatch('user/resetToken')
                    this.$router.replace('/login')
                    break;

            }
        },
        // 退出登陆
        quits() {

            this.$store.dispatch('user/resetToken')
        },
    }
}
</script>
<style scoped>
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 20px;
    background-image: linear-gradient(to right, #ffffff, #1AB8B8);

}

.headers-right-right {
    display: flex;
    justify-content: space-around;
    align-items: center;
    color: #666666;
}

.block {
    margin-right: 15px;
    display: flex;
    align-items: center;
}

.el-dropdown-link {
    cursor: pointer;
}
</style>